package college;

import java.util.ArrayList;
import java.util.List;

import clientserver.Client;
import clientserver.Packet;
import clientserver.QueryResult;
/**
 * Used for user authentication with the user list in the database. <p>
 * Holds the basic information and user types: <p>
 * @param Student - Student user type.
 * @param Lecturer - Lecturer user type.
 * @param ISE - Information System Engineer type.
 * @param ISD - Information System Director type.
 * @param ISCM - Information System Committee Manager.
 * @param ISCC - Information System Committee Chairman.
 * @author Group11
 *
 */
public class User extends collegeObject {
	/*
	 * Types of users ISE - Information System Engineer ISD - Information System
	 * Director
	 */
	public static enum UserTypes {
		Student, Lector, ISE, ISD
	}



	private String login;
	private String password;
	private String fullname;
	private String email;
	private UserTypes type;
	private boolean alreadylogged;


	// ionly one user logged in section//////////////////////////////////////////////////
	
 public void   setloggedstatus(boolean status)
 {
	 alreadylogged=status;
 }
 
 public boolean isalreadylogged()
 {
	 return alreadylogged;
 }
 
 /**
  * Check whether the user is already logged in, and update the status of the current user.
  * @param connectedclient - currently connected client (to the server)
  * @see Client
  */
 public  void updateloggedstatus(Client connectedclient)
 {
	 
	 String upd;
	 if(alreadylogged) upd="'1'"; else upd="'0'"; 
	 String q="UPDATE users SET loggedin="+upd+" WHERE login="+general.addapostrophs(login);
	 Packet p= new Packet(q);
	 p.setUpdatePacket();
	 connectedclient.SendPacketNowait(p);
 }
 //////////////////////////////getters and setters///////////////////////////////////////
 
 
 
	public static   List<User> GetUsersbyType(Client cl,String type)

	{
		
		String q="SELECT * FROM users WHERE type="+general.addapostrophs(type);

		Packet p= new Packet(q);
		
		QueryResult qr=(QueryResult) cl.SendPacketAndWait(p);
		
		

		List<User> answer = new ArrayList<User>();
		int size=qr.rowsize();
		if( size==0) return null;
		String	l; //loging
		String pas; // password
		UserTypes t; //type
	
		for(int i=0; i<size;i++)
		{
  		User newuser= new User();	
			l=qr.getString("login");
			pas=qr.getString("password");
			t=UserTypes.valueOf(qr.getString("type"));
 newuser.setLogin(qr.getString("login"));
			newuser.setPassword(pas);
			newuser.setType(t);
			answer.add(newuser);

		qr.next();
		}

		return answer;
	}
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public UserTypes getType() {
		return type;
	}

	public void setType(UserTypes type) {
		this.type = type;
	}
	
	

	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}
	
	public String toString()
	{
		String answ="User login: "+login+
				"\n User Type:"+type+
				"\n User  Full Name:"+fullname+
				"\nUser E-Mail:"+email;
		return answ;
	}
	
	
	

	
	
	
	public User() {

	}

	/**
	 * load user from Database to User Object be function call it's necessarily to set   user login with relevant function
	 *  
	 * @return true if user exist, otherwise false
	 */
	public boolean loadfromdb() {
		QueryResult qr;
		String q = "SELECT *  FROM users WHERE login='" + login + "'";
		Packet p = new Packet(q);
		System.out.println(q);
		qr = (QueryResult) client.SendPacketAndWait(p);

		if (qr.rowsize() == 0)// if no rows in server answer then user with this
								// login is not exist

		{
			return false;
		} else {
			
			password = qr.getString("password");
			 fullname=qr.getString("fullname");
			email=qr.getString("email");
			String t = qr.getString("type");
			setType(UserTypes.valueOf(t));
		
		
						 
			 
			 String lg=qr.getString("loggedin");
			
			if(lg==null||lg.equals("0")) alreadylogged=false;
			
			else
				if(lg.equals("1")) alreadylogged=true;
					
				
						
			return true;
		}

	}




}
